package com.chen.controller;

import com.chen.User;
import com.chen.feign.UserFeign;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private UserFeign userFeign;

    @RequestMapping(value = "userById/{id}",method = RequestMethod.GET)
    public User userById (@PathVariable Integer id){
        User user = userFeign.userById(id);
        return user;
        //  ServiceInstance choose = loadBalancerClient.choose("provdier");
       // String url = "http://"+choose.getHost()+":"+choose.getPort()+"/user/userById/"+id;
     //   return restTemplate.getForObject(url,User.class);
    }
    @RequestMapping(value = "userId",method = RequestMethod.GET)
    public User user(Integer id){
        User user = userFeign.userWen(id);
        return user;
    }


    @RequestMapping("users")
    public User users(@RequestBody User user){
        User users = userFeign.userU(user);
        return users;
    }

}
